package com.shigen.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shigen.demo.bean.Result;
import com.shigen.demo.entity.req.ProjectReq;
import com.shigen.demo.entity.vo.ProjectVo;
import com.shigen.demo.service.IProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "表单项目的操作接口")
@RequestMapping(value = "project")
public class ProjectController {

  @Resource
  private IProjectService projectService;

  @PostMapping(value = "create")
  @ApiOperation(value = "创建新的表单项目")
  public Result<String> insert(@RequestBody @Valid ProjectReq.OpBase opBase) {
    int i = projectService.createProject(opBase);
    return i > 0 ? Result.ok("成功") : Result.err("失败");
  }

  @PostMapping(value = "select")
  @ApiOperation(value = "根据项目名或项目描述分页查询")
  public Result<IPage<ProjectVo>> selectProjects(@RequestBody @Valid ProjectReq.QueryItem queryInfo) {
    IPage<ProjectVo> projects = projectService.getProjectList(queryInfo);
    return Result.ok(projects);
  }

  @GetMapping(value = "get/{id}")
  @ApiOperation(value = "表单项目的查询")
  public Result getProject(@PathVariable String id) {
    return projectService.getProject(id);
  }

  @PutMapping(value = "update/{id}/{isStop}")
  @ApiOperation(value = "开启和停止表单的收集")
  public Result<String> updateProjectStatus(@PathVariable("id") String id, @PathVariable("isStop") Boolean isStop) {
    int i = projectService.changeProjectStatus(id, isStop);
    return i > 0 ? Result.ok("操作成功") : Result.err("操作失败");
  }

  @GetMapping(value = "download/{id}")
  @ApiOperation(value = "下载表单数据")
  public void download(@PathVariable("id") String id, HttpServletResponse response) {
    projectService.downloadDatas(id, response);
  }
}
